package com.bayair.doctor.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.bayair.doctor.App;
import com.bayair.doctor.R;
import com.bayair.doctor.base.component.BaseActivity;
import com.bayair.doctor.biz.im_helper.Constants;
import com.bayair.doctor.ui.personal.PersonalActivity;
import com.bayair.doctor.ui.personal.PersonalLayout;
import com.bayair.doctor.ui.splash.SplashActivity;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.modules.chat.ChatLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.message.MessageLayout;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;

import butterknife.BindView;

public class ChatActivity extends BaseActivity {

    public static void actionStart(ChatInfo chatInfo) {
        Intent intent = new Intent(App.getContext(), ChatActivity.class);
        intent.putExtra(Constants.CHAT_INFO, chatInfo);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        App.getContext().startActivity(intent);
    }

    private ChatInfo mChatInfo;

    @BindView(R.id.chat_layout)
    ChatLayout mChatLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_im_chat;
    }

    @Override
    protected void initTitleBar() {
        super.initTitleBar();

        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            startSplashActivity();
        } else {
            mChatInfo = (ChatInfo) bundle.getSerializable(Constants.CHAT_INFO);
            if (mChatInfo == null) {
                startSplashActivity();
            }
        }

        //getTitleBar().getTitleView().setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        getTitleBar().setTitle(mChatInfo.getChatName());

        statusBarConfig().keyboardEnable(true).init();
    }

    @Override
    public void onRightClick(View v) {
        if (mChatInfo.getType() == TIMConversationType.C2C) {//单聊
            PersonalActivity.actionStart(mChatInfo);
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //单聊组件的默认UI和交互初始化
        mChatLayout.initDefault();
        //通过api设置ChatLayout各种属性的样例
        ChatLayoutHelper.customizeChatLayout(getActivity(), mChatLayout);
    }

    @Override
    protected void initData() {
        /*
         * 需要聊天的基本信息
         */
        mChatLayout.setChatInfo(mChatInfo);
        //获取单聊面板的标题栏
         mChatLayout.getTitleBar().setVisibility(View.GONE);
    }

    @Override
    protected void setListener() {
        mChatLayout.getMessageLayout().setOnItemClickListener(new MessageLayout.OnItemClickListener() {
            @Override
            public void onMessageLongClick(View view, int position, MessageInfo messageInfo) {
                //因为adapter中第一条为加载条目，位置需减1
                mChatLayout.getMessageLayout().showItemPopMenu(position - 1, messageInfo, view);
            }

            @Override
            public void onUserIconClick(View view, int position, MessageInfo messageInfo) {
                if (null == messageInfo) {
                    return;
                }
                ChatInfo info = new ChatInfo();
                info.setId(messageInfo.getFromUser());

                PersonalActivity.actionStart(mChatInfo);
            }
        });
    }

    private void startSplashActivity() {
        Intent intent = new Intent(getActivity(), SplashActivity.class);
        startActivity(intent);
        finish();
    }
}
